classdef compare_3_choices_prediction < experiment
    
    properties
    end
    
    methods
          function plan=test_plan1(obj)
            plan=...
                [1,   1,  1
                 14,  1, 1
                 28,  1, 2
                 34,  1, 2]; 
          end
          
          function plan=test_plan2(obj)
            plan=...
                [1,  1, 3
                 4,  2, 2
                 9,  3, 7
                14, 4, 4
                18, 5, 1
                24, 6, 5
                30, 7, 1
                35, 8, 2
                41, 9, 1
                49, 10, 2
                54, 11, 1]; 
          end
          
          function plan=test_plan3(obj)
              plan=...
               [1,   1, 3
                60, 2, 1
                110, 3, 2
                160, 4, 3
                210, 5, 5
                260, 6, 3
                310, 7, 4
                360, 8, 3
                410, 9, 4
                460, 10, 3
                510,11, 5];
        end
          
        function test(obj,training_duration,experiment_duration,plan)
            system_=system_base();
            mpc=mpc_policy(system_);
            system=system_base();
            training_duration = 50;%9*60;
            experiment_duration=training_duration+60; %540+60;
            workload = system.initialize(experiment_duration);
            s=static_policy(plan);      
            res = system.run_plan(system, training_duration,  @(t)s.staticly_configured_policy(t));
            past_util = system.past_util();
            
            start_=1;
            if (system.t-1>60)
                start_=system.t-60;
            end
            util_interval =  past_util((start_:end),:);
            u=UtilityLib(); figure;
            subplot(2,1,1);   plot((start_:system.t-1)', util_interval, u.color{1}); hold on;
            %             inst =  system.past_future_inst();
            %             subplot(2,1,2); plot((start_:system.t+60-1)', inst(start_:system.t+60-1,:));
            
            % do estimation -------------------
           m=mpc.build_model( system.past_inst(), system.past_util());
            choice=[1 0 0 0 0 0 0 0 0 0 0 ];
            prediction = mpc.predict_choice_outcome(m,choice,  system.past_future_inst(), system.past_util(), system.t-1 );
            prediction.util = max(prediction.util,zeros(size(prediction.util)));
            subplot(2,1,1); plot((system.t-1:system.t+60-1)', prediction.util,  u.color{2});
            subplot(2,1,2); plot((start_:system.t+60-1)', prediction.inst);
            % k = waitforbuttonpress
                        
              choice=[2 0 0 0 0 0 0 0 0 0 0 ];
                prediction = mpc.predict_choice_outcome(m,choice,  system.past_future_inst(), system.past_util(), system.t-1 );               
                prediction.util = max(prediction.util,zeros(size(prediction.util)));
               subplot(2,1,1); plot((system.t:system.t+60)', prediction.util,  u.color{3});
              % subplot(2,1,2); plot((system.t-60:system.t+60)',res.inst);
               % k = waitforbuttonpress

              choice=[3 0 0 0 0 0 0 0 0 0 0 ];
                prediction = mpc.predict_choice_outcome(m,choice,  system.past_future_inst(), system.past_util(), system.t-1 );               
                prediction.util = max(prediction.util,zeros(size(prediction.util)));
               subplot(2,1,1); plot((system.t:system.t+60)', prediction.util,  u.color{4});
              % subplot(2,1,2); plot((system.t-60:system.t+60)',res.inst);
            
            max_util=max(max(prediction.util),max(system.past_util()));
            subplot(2,1,1); line([system.t system.t],[0 max_util],...
                'Color',u.color{1},...
                'LineStyle',u.lnstyle{1},...
                'LineWidth',2,...
                'Marker','s',...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','g',...
                'MarkerSize',6);
                axis([0 system.t+60 0 max_util]);
%             inst =  system.past_future_inst();
%            subplot(3,1,3); plot((start_:system.t-1)', inst(start_:system.t-1,:));
            
            % plot((system.t-60:system.t)', res.util_est);
            %               subplot(2,1,2); plot((system.t-60:system.t+60)',res.inst);
            %                 subplot(4,1,3); plot( system.past_future_inst()  );
            %                 subplot(4,1,4); plot(past_util); hold on; plot((system.t-60:system.t+60)',yp);
        end
        
       function run_test(obj)
            training_duration = 9*60;
            experiment_duration=training_duration+60; %540+60;
            obj.test(training_duration,experiment_duration,@obj.test_plan3);
        end

    end
    
end

